﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BooksManagermentSystem
{
    public partial class UpdateBookType_form : Form
    {
        public UpdateBookType_form()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string typename = comboBox1.Text;
            string newname = textBox1.Text.Trim();
            if(newname == "")
            {
                MessageBox.Show("空的类型！");
                textBox1.Focus();
                return;
            }

            DialogResult rt = MessageBox.Show($"确定要把【{typename}】改为【{newname}】吗？", "提示", MessageBoxButtons.YesNo);
            if(rt == DialogResult.Yes)
            {
                string sql = $"update BookType set typename = '{newname}' where typename= '{typename}'";
                if(DBhelper.NonQuery(sql))
                {
                    MessageBox.Show("修改成功");
                    ShowAllBookType();
                }
                else
                {
                    MessageBox.Show("修改失败！");
                    textBox1.Focus();
                }
            }
        }

        private void UpdateBookType_form_Load(object sender, EventArgs e)
        {
            ShowAllBookType();
        }

        private void ShowAllBookType()
        {
            string sql = "select * from BookType";
            DataTable dt = DBhelper.GetDataTable(sql);
            comboBox1.DataSource= dt;
            comboBox1.DisplayMember= "typename";
        }
    }
}
